﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OdeToFood.Models;

namespace OdeToFood.Controllers
{
    public class RestaurantController : Controller
    {
        //
        // GET: /Restaurant/

        OdeToFoodDB _db = new OdeToFoodDB();

        public ActionResult Index(string calle, string ciudad)
        {
            ViewBag.Ciudades = _db.Restaurants.Select(r => r.Address.city).Distinct();
            ViewBag.Calles = _db.Restaurants.Select(r => r.Address.street).Distinct();
            
            /* var model = from r in _db.Restaurants
                        orderby r.Address.street
                        select r; */
            
            var model = from r in _db.Restaurants
                        orderby r.Address.street descending
                        where r.Address.street == calle || (calle == null) 
                        where r.Address.city == ciudad || (ciudad == null)
                        select r;

            return View(model);            
        }

        //
        // GET: /Restaurant/Details/5

        public ActionResult Details(int id)
        {
            var model = _db.Restaurants.Single(r => r.ID == id);            
            return View(model);
        }

        //
        // GET: /Restaurant/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Restaurant/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Restaurant/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Restaurant/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Restaurant/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Restaurant/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
